#include <jni.h>
#include <thehilog/log.h>

extern "C" {
static void __attribute__((used)) somefakefunc(void) { }

int crash_write_read_only() {
    volatile char * ptr = (char *)somefakefunc;
    *ptr = 0;
    return 5;
}

volatile unsigned uint_f2wk124_dont_optimize_me_bro;

_Noreturn void trigger_anr() {
    for (unsigned i = 0; ; i++) {
        uint_f2wk124_dont_optimize_me_bro = i;
    }
}


JNIEXPORT void JNICALL
Java_com_example_bugsnag_ohos_BaseCrashyAbility_crashFromCXX(JNIEnv * env, jobject instance) {
    HILOG_INFO(LOG_APP, "bugsdemo c++ crashFromCXX start...");
    crash_write_read_only();
}

JNIEXPORT void JNICALL
Java_com_example_bugsnag_ohos_BaseCrashyAbility_anrFromCXX(JNIEnv * env, jobject instance) {
    HILOG_INFO(LOG_APP, "bugsdemo c++ anrFromCXX start...");
    trigger_anr();
}

}
